#pragma once

#include "stdafx.h"
#include "GbrHttpReqResp.h"

class CGbrHttpServ :
	public CGbrHttpReqResp
{
private:
	CGbrHttpServ(void) {}
public:
	CGbrHttpServ(const wchar_t *fullyQualifiedUrl);
	~CGbrHttpServ(void);
	DWORD DoReceiveRequests_SingleThreaded();
	ULONG CurrentError();
	ULONG SendAndLog(const USHORT statusCode, PCSTR pStrSendReason, PCSTR pStrSendContent, PSTR pStrLog, PCWSTR pWstrLog);
protected:
	HANDLE hReqQueue;
	HTTP_RESPONSE response;
	HTTP_REQUEST_ID requestId;
	USHORT status;
	PSTR reason;
	void Initialize();
    ULONG RequestBufferLength;
    PHTTP_REQUEST pRequest;
private:
	const wchar_t *qualifiedUrl;
	ULONG currentError;
};

static char HTML_NO_DATA_PROLOG[] = "<html><body>No data</br>";
static char HTML_NO_DATA_EPILOG[] = "</body></html>";
static char HTML_REFRESH_FAILED[] = "<html><body>Refresh failed.</body></html>";
static char HTML_REFRESH_SUCCEEDED[] = "<html><body>Refresh succeeded.</body></html>";

static char USAGE_STRING[] = "<html><body>\n"
	"Usage: \"http://localhost:88/GbrServ/cmd...\n<br>"
	"Usage: \"http://localhost:88/GbrServ/sql_txt_html_table/options/instance/database/Query\n<br>"
	"Sample: \"sql_txt_html_table/localhost/r10/msdb/select object_id,name from sys.tables order by 2\" should show table names, buffering 10 rows at the time from msdb as text/html\n<br>"
	"I got: \"%s\"<br></body></html>";

static char HTML_HEAD[] = "<html><body><table border=\"1\">";
static char HTML_PRE_RECORD[] = "<tr>";
static char HTML_BETWEEN_FIELDS[] = "</td><td>";
static char HTML_EPI_RECORD[] = "</td>";
static char HTML_TAIL[] = "</table></body></html>";

